/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-04-25
 * Time: 15:58
 */
public class BubbleSort {
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        // n - 1 为排序的趟数
        for(int i = 0; i < n - 1; i++) {
            boolean flag = true;
            // n - 1 - i 为每趟需要牌的数据个数
            for(int j = 0; j < n - 1 - i; j++) {
                if(arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    flag = false;
                }
            }
            if(flag) {
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        bubbleSort(arr);
        for(int i : arr) {
            System.out.print(i + " ");
        }
    }
}
